<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

class igprofileModelIgprofile extends igbaseModel
{
	protected $context = 'igprofiles';
	protected $tablename = 'igallery_profiles';
	protected $filterOrderDefault = 'ordering';
	protected $reorderClause = '';
	    
	
	function __construct($task)
	{
		parent::__construct();
	}
	
	function _buildQuery()
	{
		$where		= $this->_buildContentWhere();
		$orderby	= $this->_buildContentOrderBy();
        $query = 'SELECT * from #__igallery_profiles ' . $where . $orderby;
        return $query;
	}
	
	function _buildContentWhere()
	{
		global $mainframe, $option;
		
		$db					=& JFactory::getDBO();
		
		$filter_state		= $mainframe->getUserStateFromRequest( $this->context.'.filter_state',		'filter_state',		'',				'word' );
		$filter_order		= $mainframe->getUserStateFromRequest( $this->context.'.filter_order',		'filter_order',		'ordering',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $this->context.'.filter_order_Dir',	'filter_order_Dir',	'',				'word' );
		$search				= $mainframe->getUserStateFromRequest( $this->context.'.search',			    'search',			'',				'string' );
		$search				= JString::strtolower( $search );

		$where = array();

		if ($search) 
		{
			$where[] = 'name LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
		}
		if ( $filter_state ) 
		{
			if ( $filter_state == 'P' ) 
			{
				$where[] = 'published = 1';
			} 
			else if ($filter_state == 'U' ) 
			{
				$where[] = 'published = 0';
			}
		}

		$where 		= ( count( $where ) ? ' WHERE '. implode( ' AND ', $where ) : '' );

		return $where;
	}
	
	function checkAssigned()
	{ 
		$query = 'SELECT id, name from #__igallery where profile = '.(int)$this->_id;
		$this->_db->setQuery($query);
		$categories = $this->_db->loadObjectlist();

		if( !empty($categories) )
		{
			$msg = '';
			for($i=0; $i<count($categories); $i++)
			{
				$msg .= ' <a href="index.php?option=com_igallery&amp;controller=igcategories&amp;view=add_edit_category&amp;cid='.$categories[$i]->id.'">'.$categories[$i]->name.'</a> ,';
			}

			JError::raise(2, 500, JText::_('PLEASE REASSIGN').$msg);
			return false;
		}
		
		return true;
	}
	
	function save($post)
	{
		$row =& $this->getTable('igallery_profiles');
        $row->id = (int)$this->_id;
        
		if (!$row->bind($post)) 
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		if(!$row->id) 
		{
			$row->ordering = $row->getNextOrder();
		}
		
		$row->acl_cat_menu = implode(',', $row->acl_cat_menu);
		$row->acl_cat_display = implode(',', $row->acl_cat_display);
		$row->acl_cat_view = implode(',', $row->acl_cat_view);
		$row->acl_cat_edit = implode(',', $row->acl_cat_edit);
		$row->acl_cat_delete = implode(',', $row->acl_cat_delete);
		$row->acl_cat_publish = implode(',', $row->acl_cat_publish);
		$row->acl_cat_order = implode(',', $row->acl_cat_order);
		$row->acl_upload = implode(',', $row->acl_upload);
		$row->acl_photo_view = implode(',', $row->acl_photo_view);
		$row->acl_photo_edit = implode(',', $row->acl_photo_edit);
		$row->acl_photo_del = implode(',', $row->acl_photo_del);
		$row->acl_photo_publish = implode(',', $row->acl_photo_publish);
		$row->acl_photo_ordering = implode(',', $row->acl_photo_ordering);
		
		if($post['remove_wm_image'] == 1)
		{
		    $query = 'SELECT watermark_filename FROM #__igallery_profiles WHERE id = '.(int)$this->_id;
            $this->_db->setQuery($query);
            $this->_profile = $this->_db->loadObject();
		    
		    $query = 'SELECT watermark_filename FROM #__igallery_profiles WHERE watermark_filename = '.$this->_db->Quote($this->_profile->watermark_filename);
    		$this->_db->setQuery($query);
    		$this->_db->query();
    		$numRows = $this->_db->getNumRows();
    		$deleteImg = $numRows > 1 ? false: true;
    		
    		if($deleteImg)
    		{
    	       JFile::delete(IG_WATERMARK_PATH.DS.$this->_profile->watermark_filename);
    		}
    	    
    	    $row->watermark_filename =  '';
		}
		
		
        if (!$row->store()) 
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		

		return true;
	}
	
	function delete()
	{
	    $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		
		for($i=0; $i<count($cid); $i++)
		{
		    $query = 'SELECT watermark_filename FROM #__igallery_profiles WHERE id = '.(int)$cid[$i];
            $this->_db->setQuery($query);
            $this->_profile = $this->_db->loadObject();
		
    		$query = 'SELECT watermark_filename FROM #__igallery_profiles WHERE watermark_filename = '.$this->_db->Quote($this->_profile->watermark_filename);
    		$this->_db->setQuery($query);
    		$this->_db->query();
    		$numRows = $this->_db->getNumRows();
    		if($numRows < 2)
    		{
    		    JFile::delete(IG_WATERMARK_PATH.DS.$this->_profile->watermark_filename); 
    		}
			
			$query = 'DELETE FROM #__igallery_profiles WHERE id = '.(int)$cid[$i];
			$this->_db->setQuery($query);
			if(!$this->_db->query()) 
			{
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}

		return true;
	}
    
	function copy()
	{
	    $row =& $this->getTable('igallery_profiles');
	    $row->load($this->_id);
        $row->id = null;
        $row->ordering = $row->getNextOrder();
        $row->name = JText::_('COPY OF').' '.$row->name;
        if (!$row->store())
		{
		    $this->setError( $row->getError() );
			return false;
		}
		return true;
	}
}